package com.gotop.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gotop.common.base.BaseController;
import com.gotop.common.constant.Kv;
import com.gotop.common.utils.R;
import com.gotop.common.utils.auth.CemUser;
import com.gotop.entity.system.Tenant;
import com.gotop.system.service.system.ITenantService;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @program: cem
 * @description:
 * @author: superfish
 * @create: 2020-12-25 11:19
 **/
@RestController
@AllArgsConstructor
@RequestMapping("/tenant")
@ApiIgnore
@Api(value = "租户管理", tags = "接口")
public class TenantController extends BaseController {

    @Autowired
    private ITenantService tenantService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperation(value = "详情", notes = "传入tenant")
    public R<Tenant> detail(Tenant tenant) {
        Tenant detail = tenantService.getByTenantId(tenant.getTenantId());
        return R.data(detail);
    }

    /**
     * 分页
     */
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "参数名称", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "tenantName", value = "角色别名", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "contactNumber", value = "联系电话", paramType = "query", dataType = "string")
    })
    @ApiOperation(value = "分页", notes = "传入tenant")
    public R<IPage<Tenant>> list(@ApiIgnore @RequestParam Map<String, Object> tenant) {
        return R.data(null);
    }

    /**
     * 下拉数据源
     */
    @GetMapping("/select")
    @ApiOperation(value = "下拉数据源", notes = "传入tenant")
    public R<List<Tenant>> select(Tenant tenant, CemUser cemUser) {
        return R.data(null);
    }

    /**
     * 自定义分页
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页", notes = "传入tenant")
    public R<IPage<Tenant>> page(Tenant tenant) {
        return R.data(null);
    }

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @ApiOperation(value = "新增或修改", notes = "传入tenant")
    public R submit(@Valid @RequestBody Tenant tenant) {
        return R.status(tenantService.saveTenant(tenant));
    }


    /**
     * 删除
     */
    @PostMapping("/remove")
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        final String[] split = ids.split(",");
        List<Long> longs = new LinkedList<>();
        for (String s : split) {
            longs.add(Long.parseLong(s));
        }
        return R.status(tenantService.deleteLogic(longs));
    }

    /**
     * 根据域名查询信息
     *
     * @param domain 域名
     */
    @GetMapping("/info")
    @ApiOperation(value = "配置信息", notes = "传入domain")
    public R<Kv> info(String domain) {
        Tenant tenant = tenantService.getOne(Wrappers.<Tenant>query().lambda().eq(Tenant::getDomain, domain));
        Kv kv = Kv.init();
        if (tenant != null) {
            kv.set("tenantId", tenant.getTenantId()).set("domain", tenant.getDomain());
        }
        return R.data(kv);
    }


}